package com.wandian.cqcsk.tools;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.customapi.CustomServiceApi;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;

import java.io.IOException;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.mylib.libcore.config.NetConfig.NET_BASE;
import static com.mylib.libcore.config.NetConfig.NET_IS_LOCAL;


/**
 * @author cl
 * @date 2020/12/11
 * @description
 **/
public class ActivitySelfManager implements Application.ActivityLifecycleCallbacks {
    private static CustomServiceApi customServiceApi;
    private long noTime = 0;
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Log.d("ActivitySelfManager","onActivityCreated");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.d("ActivitySelfManager","onActivityStarted");

    }

    @Override
    public void onActivityResumed(Activity activity) {
        Log.d("ActivitySelfManager","onActivityResumed");
    }

    @Override
    public void onActivityPaused(Activity activity) {
        Log.d("ActivitySelfManager","onActivityPaused");
        postAppLifeTime(activity);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        Log.d("ActivitySelfManager","onActivityStopped");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Log.d("ActivitySelfManager","onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
       // removeContentView(relativeLayout);
        Log.d("ActivitySelfManager","onActivityDestroyed");
    }

    //计算app在线使用时长
    public void postAppLifeTime(Activity activity) {
        try {
            if(System.currentTimeMillis()-noTime>30*1000){
                noTime = System.currentTimeMillis();
                long appStartTime = Long.parseLong(AppContext.getInstance().getAppPreferencesHelper().getAppStartTime()+"");
                long appStopTime = System.currentTimeMillis()/1000;
                if(Math.abs(appStopTime-appStartTime)>2*60){
                    long time = Math.abs(appStopTime-appStartTime);
                    if(time>10*60) time = 10*60;
                    //成功重新开始时长
                    AppContext.getInstance().getAppPreferencesHelper().setAppStartTime();
                    //发送请求
                    if(UIUtils.isLogin())sendOnlineTime(time/60+"");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //上报使用时长
    public void sendOnlineTime(String time){
      try {
          String wdt = AppContext.getInstance().getAppPreferencesHelper().getToken();
          if(UIUtils.isEmpty(wdt))return;
          HashMap<String, String> hashMap = new HashMap<>();
          hashMap.put("time",time);
          hashMap.put("wdt",wdt);
          OkHttpClient okHttpClient = new OkHttpClient();
          MediaType type = MediaType.parse("application/json;charset=utf-8");
          JsonObject jsonObject = new JsonObject();
          RequestBody requestBody = null;
          if(NET_IS_LOCAL) {
              //去掉加密key
              requestBody =  RequestBody.create(type,""+JsonUtil.toJson(hashMap));
          }else {
              jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
              requestBody =  RequestBody.create(type,""+jsonObject.toString());
          }
//          RequestBody requestBody = new FormBody.Builder()
//                  .add("wdk",AESUtils.getInstance().encrypt(hashMap))
//                  .build();
          Request request = new Request.Builder()
                  .url(NET_BASE + "uApp/user/addOnlineTime")
                  .header("dt", "android")
                  .header("wdt", wdt)
                  .post(requestBody)
                  .build();
          //异步请求
          okHttpClient.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
              }
              @Override
              public void onResponse(Call call, Response response) throws IOException {
              }
          });

      }catch (Exception e){}
    }
}